@using Microsoft.AspNetCore.Html
@using Microsoft.AspNetCore.Mvc.Localization
@using OrchardCore.DisplayManagement
@using System.Collections
@using System.Runtime.CompilerServices

@{
    var image = (Model.Image as object)?.ToString();
    var imageAlt = string.IsNullOrWhiteSpace($"{Model.Alt}")
        ? image?.Split('/')[^1].Split('?')[0]
        : Model.Alt;
    var imageBottom = Model.ImageBottom == true ||
        (bool.TryParse($"{Model.ImageBottom}", out var imageBottomBool) && imageBottomBool);

    var classes =
        (Model as IShape)?.Classes ??
        (Model as IEnumerable)?.CastWhere<object>().SelectWhere(item => item.ToString()) ??
        [];

    object NotEmpty(object item)
    {
        var text = item is IHtmlContent htmlContent ? htmlContent.Html() : item?.ToString();
        return string.IsNullOrWhiteSpace(text) ? null : item;
    }
}

@* Based on https://getbootstrap.com/docs/5.3/components/card/. *@
<div class="bootstrapCard card @string.Join(' ', classes)">
    @if (!imageBottom && !string.IsNullOrWhiteSpace(image))
    {
        <div class="bootstrapCardImage">
            <img src="@Url.Content(image)" class="card-img-top" alt="@imageAlt">
        </div>
    }

    @if (NotEmpty(Model.Header) is { } header)
    {
        <div class="card-header">
            @header
        </div>
    }

    <div class="card-body">
        @if (NotEmpty(Model.Title) is { } title)
        {
            <h5 class="card-title">@title</h5>
        }

        @if (NotEmpty(Model.Subtitle) is { } subtitle)
        {
            <h6 class="card-subtitle mb-2 text-body-secondary">@subtitle</h6>
        }

        @if (NotEmpty(Model.Text) is { } text)
        {
            <p class="card-text">@text</p>
        }

        @if (Model.Content is IHtmlContent content)
        {
            @content
        }

        @if (Model.Links is IEnumerable links)
        {
            foreach (var parts in links.CastWhere<ITuple>(link => link.Length > 1))
            {
                var linkUrl = parts[0]?.ToString() ?? string.Empty;
                object linkText = parts[1];

                <a href="@(string.IsNullOrWhiteSpace(linkUrl) ? "#" : Url.Content(linkUrl))" class="card-link">@linkText</a>
            }
        }
    </div>

    @if (Model.List is IEnumerable list)
    {
        <ul class="list-group list-group-flush">
            @foreach (var item in list)
            {
                <li class="list-group-item">
                    @item
                </li>
            }
        </ul>
    }

    @if (NotEmpty(Model.Footer) is { } footer)
    {
        <div class="card-footer">
            @footer
        </div>
    }

    @if (imageBottom && !string.IsNullOrWhiteSpace(image))
    {
        <div class="bootstrapCardImage">
            <img src="@Url.Content(image)" class="card-img-bottom " alt="@imageAlt">
        </div>
    }
</div>
